package bx.job.jobstore.processor;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

public class DataSourceProcessor {
    private static volatile DataSource DATASOURCE;
    private static Config AKKA_GLOBAL_CONFIG = ConfigFactory.load().getConfig("bx-job-store-mysql.db");
    private static String DB_URL = AKKA_GLOBAL_CONFIG.getString("url");
    private static String DB_USERNAME = AKKA_GLOBAL_CONFIG.getString("username");
    private static String DB_PASSWORD = AKKA_GLOBAL_CONFIG.getString("password");
    private static String DB_DRIVER_NAME = AKKA_GLOBAL_CONFIG.getString("drivername");

    public static DataSource getDataSource() {
        if (DATASOURCE == null) {
            synchronized (DataSourceProcessor.class) {
                if (DATASOURCE == null) {
                    HikariDataSource dataSource = new HikariDataSource();
                    dataSource.setJdbcUrl(DB_URL);
                    dataSource.setDriverClassName(DB_DRIVER_NAME);
                    dataSource.setUsername(DB_USERNAME);
                    dataSource.setPassword(DB_PASSWORD);
                    dataSource.setIdleTimeout(60000);
                    dataSource.setConnectionTestQuery("SELECT 1");
                    DATASOURCE = dataSource;
                }
            }
        }
        return DATASOURCE;
    }
}
